﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using BE;
using BC;

namespace ModuleDuoc
{
    public partial class frmPhieuTraLai : Form
    {
        string _soPhieu;// = "HDBL-220513-7141";

        int sl;
        //string _kh;
        //string _dt;
        //string _dc;
        //string _user;

        public frmPhieuTraLai()
        {
            InitializeComponent();
        }
        public frmPhieuTraLai(string kh, string dt, string dc, string user, string sp)
        {
            InitializeComponent();
            txtKhachHang.Text = kh;
            txtDiaChi.Text = dc;
            txtDienThoai.Text = dt;
            txtNguoiTao.Text = user;
            _soPhieu = sp;
        }

        private void frmPhieuTraLai_Load(object sender, EventArgs e)
        {
            getDonHang();
        }
        private void getDonHang()
        {
            dgvData.Rows.Clear();
            try
            {
                string sql = "SELECT Expr1, Ten, mavach, soluong, dongia " +
                             "FROM [QUANLYVTXN].[dbo].[hien_rpt_BanLe] " +
                             "WHERE sophieu='" + _soPhieu + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.dgvData.Rows.Add(dt.Rows[i]["Expr1"].ToString(), dt.Rows[i]["Ten"].ToString(), dt.Rows[i]["MaVach"].ToString(), dt.Rows[i]["SoLuong"].ToString(), dt.Rows[i]["DonGia"].ToString(), 0, "", "");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void tinhGia2() // Tính giá khi thay đổi số lượng trên lưới
        {
            if (this.dgvData.Rows.Count > 0)
            {
                try
                {
                    Double dg;
                    Double tt;
                    for (int i = 0; i < this.dgvData.Rows.Count; i++)
                    {
                        if (this.dgvData.Rows[i].Cells["SLTra"].Value.ToString() != "")
                        {
                            sl = Int32.Parse(this.dgvData.Rows[i].Cells["SLTra"].Value.ToString());
                            dg = Double.Parse(this.dgvData.Rows[i].Cells["DonGia"].Value.ToString());
                            tt = (sl * dg);
                            this.dgvData.Rows[i].Cells["thanhtien"].Value = String.Format("{0:0,0}", tt);
                        }
                    }
                }
                catch { }
            }
            this.getTongGia();
        }
        private void getTongGia()
        {
            try
            {
                Double tt = 0;
                for (int i = 0; i < this.dgvData.Rows.Count; i++)
                {
                    tt += Double.Parse(this.dgvData.Rows[i].Cells["thanhtien"].Value.ToString());
                }
                this.txtTongTien.Text = String.Format("{0:0,0}", tt);
            }
            catch { }
        }

        private void txtSoPhieu_TextChanged(object sender, EventArgs e)
        {
            getDonHang();
        }

        private void dgvData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (Int32.Parse(this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["SLTra"].Value.ToString()) < 0)
                {
                    MessageBox.Show("Không được nhập âm!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["SLTra"].Value = 0;
                }
            }
            catch { this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["SLTra"].Value = 0; }
            tinhGia2();
        }

        private void insetPhieuTraLai()
        {
            try
            {
                if (float.Parse(txtTongTien.Text) > 0)
                {
                    ZD_PhieuTraLai beTraLai = new ZD_PhieuTraLai();
                    ZD_PhieuTraLaiBC facTraLai = new ZD_PhieuTraLaiBC();
                    

                    beTraLai.NgayLap = DateTime.Now;
                    beTraLai.TenNhanVien = txtNguoiTao.Text;
                    beTraLai.TongTien = float.Parse(txtTongTien.Text);
                    beTraLai.SoHoaDonBanLe = _soPhieu;
                    beTraLai.GhiChu = txtGhiChu.Text;
                    beTraLai.KhachHang = txtKhachHang.Text;
                    beTraLai.DienThoai = txtDienThoai.Text;
                    beTraLai.DiaChi = txtDiaChi.Text;
                    beTraLai.DuyetPhieu = 0;

                    facTraLai.Insert(beTraLai);
                    insertPhieuTraLaiCT();

                    if (MessageBox.Show("Thêm mới thành công! Bạn có muốn in phiếu này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.printReport();
                    }
                }
                else
                {
                    MessageBox.Show("Phiếu không hợp lệ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch { MessageBox.Show("Phiếu không hợp lệ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
        }

        private void insertPhieuTraLaiCT()
        {
            ZD_PhieuTraLaiCT beTraLaiCT = new ZD_PhieuTraLaiCT();
            ZD_PhieuTraLaiCTBC facTraLaiCT = new ZD_PhieuTraLaiCTBC();
            for (int i = 0; i < dgvData.Rows.Count; i++)
            {
                if (Int32.Parse(dgvData.Rows[i].Cells["SLTra"].Value.ToString()) > 0)
                {
                    beTraLaiCT.SoHoaDonBanLe = _soPhieu;
                    beTraLaiCT.MaVach = Int32.Parse(dgvData.Rows[i].Cells["MaVach"].Value.ToString());
                    beTraLaiCT.TenVatTu = dgvData.Rows[i].Cells["Expr1"].Value.ToString();
                    beTraLaiCT.SoLo = "";
                    beTraLaiCT.DonViTinh = dgvData.Rows[i].Cells["Ten"].Value.ToString();
                    beTraLaiCT.SoLuongMua = Int32.Parse(dgvData.Rows[i].Cells["SoLuong"].Value.ToString());
                    beTraLaiCT.DonGia = float.Parse(dgvData.Rows[i].Cells["DonGia"].Value.ToString());
                    beTraLaiCT.SoLuongTraLai = Int32.Parse(dgvData.Rows[i].Cells["SLTra"].Value.ToString());
                    beTraLaiCT.ThanhTien = float.Parse(dgvData.Rows[i].Cells["ThanhTien"].Value.ToString());
                    beTraLaiCT.GhiChu = dgvData.CurrentRow.Cells["GhiChu"].Value.ToString();

                    facTraLaiCT.Insert(beTraLaiCT);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (checkExitSoPhieu() == false)
            {
                insetPhieuTraLai();
            }
            else
            {
                MessageBox.Show("Phiếu này đã được tạo!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void txtTongTien_TextChanged(object sender, EventArgs e)
        {
            this.lblBangChu.Text = DICHSO.XL_DOC_SO.DOC_SO_CHUOI(Double.Parse(this.txtTongTien.Text.ToString()), "đồng", "", 0);
        }

        private void btnXoaChon_Click(object sender, EventArgs e)
        {
            for (int i = this.dgvData.SelectedRows.Count - 1; i >= 0; i--)
            {
                this.dgvData.Rows.RemoveAt(this.dgvData.SelectedRows[i].Index);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            printReport();
        }
        private void printReport()
        {
            string sql = "SELECT * FROM [QUANLYVTXN].[thongke].[hien_rpt_TraLai] " +
                         "WHERE SoHoaDonBanLe = N'"+_soPhieu+"'";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            frmShowReport frm = new frmShowReport(2);
            frmShowReport._thanhtien = lblBangChu.Text;
            frmShowReport._date1 = String.Format("{0:dd/MM/yyyy}", DateTime.Now.ToShortDateString()); 
            frmShowReport._dt = dt;
            frmShowReport.strC = frmShowReport.BC.PHIEUTRALAI;
            frm.ShowDialog();
        }
        private bool checkExitSoPhieu()
        {
            string sql = "SELECT * FROM [QUANLYVTXN].[thongke].[hien_rpt_TraLai] " +
                         "WHERE SoHoaDonBanLe = N'" + _soPhieu + "'";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            { return false; }
        }
    }
}
